const BASE_URL = "https://jfphp.tswld.com/api";
export const get = options => {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: '加载中'
		});
		uni.request({
			url: BASE_URL + options.url,
			data: options.data || {},
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'Authorization': 'bearer ' + uni.getStorageSync("token")
			},
			method: "GET",
			success: (res) => {
				uni.hideLoading()
				if (res.data.code == 10010 || res.data.code == 10001 || res.data.code ==
					10013 || res.data.code == 20003 || res.data.code == 20002 || res.data
					.code == 20000) {
					uni.showToast({
						icon: "none",
						title: "请重新登录"
					})
					setTimeout(() => {
						uni.redirectTo({
							url: "/pages/index/index"
						})
						localStorage.clear()
					}, 10)
				}
				resolve(res.data)
			},
			fail: error => {
				uni.hideLoading() // 先调用hide
				uni.showToast({ //showToast放到队列的末尾
					title: '加载失败',
					duration: 2000
				})
				reject(error)
			}
		})
	})
}
export const put = options => {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: '加载中'
		});
		uni.request({
			url: BASE_URL + options.url,
			data: options.data || {},
			header: {
				'content-type': 'application/json',
				'Authorization': 'bearer ' + uni.getStorageSync("token")
			},
			method: "PUT",
			success: (res) => {
				uni.hideLoading()
				if (res.data.code == 10010) {
					uni.showToast({
						icon: "none",
						title: "请重新登录"
					})
					setTimeout(() => {
						uni.redirectTo({
							url: "/pages/login/login"
						})
					}, 10)
				}
				resolve(res.data)
			},
			fail: error => {
				uni.hideLoading() // 先调用hide
				uni.showToast({ //showToast放到队列的末尾
					title: '加载失败',
					duration: 2000
				})
				reject(error)
			}
		})
	})
}
export const del = options => {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: '加载中'
		});
		uni.request({
			url: BASE_URL + options.url,
			data: options.data || {},
			header: {
				'content-type': 'application/json',
				'Authorization': 'bearer ' + uni.getStorageSync("token")
			},
			method: "DELETE",
			success: (res) => {
				uni.hideLoading()
				if (res.data.code == 10010) {
					uni.showToast({
						icon: "none",
						title: "请重新登录"
					})
					setTimeout(() => {
						uni.redirectTo({
							url: "/pages/login/login"
						})
					}, 10)
				}
				resolve(res.data)
			},
			fail: error => {
				uni.hideLoading() // 先调用hide
				uni.showToast({ //showToast放到队列的末尾
					title: '加载失败',
					duration: 2000
				})
				reject(error)
			}
		})
	})
}
export const post = options => {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: '加载中'
		});
		uni.request({
			url: BASE_URL + options.url,
			data: options.data || {},
			header: {
				'content-type': 'application/json',
				'Authorization': 'bearer ' + uni.getStorageSync("token")
			},
			method: "POST",
			success: (res) => {
				uni.hideLoading()
				if (res.data.code == 10010) {
					uni.showToast({
						icon: "none",
						title: "请重新登录"
					})
					setTimeout(() => {
						uni.redirectTo({
							url: "/pages/login/login"
						})
					}, 10)
				}
				resolve(res.data)
			},
			fail: error => {
				uni.hideLoading() // 先调用hide
				uni.showToast({ //showToast放到队列的末尾
					title: '加载失败',
					duration: 2000
				})
				reject(error)
			}
		})
	})
}